package aom.ju.ss.view.cut

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.viewpager.widget.ViewPager
import aom.ju.ss.R
import aom.ju.ss.databinding.CutHomeFragmentBinding
import aom.ju.ss.databinding.CutIngItemBinding
import aom.ju.ss.mode.HomeCatInfo
import aom.ju.ss.mode.HomeCatInfoItem
import aom.ju.ss.utils.VG
import aom.ju.ss.utils.dp
import aom.ju.ss.utils.http.LP_API.cut_page_info
import aom.ju.ss.utils.http.OKHttpUtils
import aom.ju.ss.utils.show
import aom.ju.ss.utils.start
import aom.ju.ss.view.base.BaseFragment
import aom.ju.ss.view.base.PAdapter
import aom.ju.ss.view.base.TopBar
import com.google.gson.Gson

/**
 * 砍价 首页
 */
class CutHomeFragment : BaseFragment() {
    companion object {
        fun getMys(): CutHomeFragment {
            return CutHomeFragment()
        }
    }

    private lateinit var binding: CutHomeFragmentBinding

    private var fs = arrayListOf<Fragment>()

    private lateinit var cutAdapter: PAdapter

    private var cutList = arrayListOf<HomeCatInfoItem>()

    override fun onResume() {
        super.onResume()
        catHeadInfo()
    }

    override fun init() {
        binding = viewBinding as CutHomeFragmentBinding
        binding.rootView.setPadding(0, TopBar.getStatusBarHeight(activity) + 12.dp, 0, 0)


        fs.add(CutGoodsFragment.getMys("2"))
        fs.add(CutGoodsFragment.getMys("3"))
        fs.add(CutGoodsFragment.getMys("1"))

        binding.vp.adapter = object : FragmentStatePagerAdapter(childFragmentManager, 1) {
            override fun getCount(): Int {
                return fs.size

            }

            override fun getItem(position: Int): Fragment {
                return fs[position]
            }
        }
        binding.p = 0
        binding.vp.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {


            }

            override fun onPageSelected(position: Int) {
                binding.p = position
            }

            override fun onPageScrollStateChanged(state: Int) {
            }

        })
        binding.ss.setOnClickListener { }
        binding.car.setOnClickListener { }
        binding.msg.setOnClickListener { }
        binding.getKanye.setOnClickListener { }

        binding.kanjia.setOnClickListener { binding.vp.currentItem = 0 }
        binding.qq.setOnClickListener { binding.vp.currentItem = 1 }
        binding.pp.setOnClickListener { binding.vp.currentItem = 2 }

        catHeadInfo()

        binding.cutRec.layoutManager = LinearLayoutManager(activity)
        cutAdapter = PAdapter(
            cutList, R.layout.cut_ing_item
        ) { binding, position ->
            val b = binding as CutIngItemBinding
            b.info = cutList[position]
            b.cut.setOnClickListener {
                start(HelpCutDetailActivity::class.java, cutList[position].id)
            }
            b.line.view.VG(position != cutList.size - 1)

        }
        binding.cutRec.adapter = cutAdapter
        binding.lookAll.setOnClickListener {
            start(CutIngListActivity::class.java)
        }
    }

    /**
     * 头部信息
     */
    private fun catHeadInfo() {
        OKHttpUtils(activity, false)
            .SetApiUrl(cut_page_info)
            .POST(this)
    }


    override fun ok(jsonString: String?, httpTY: String?) {
        super.ok(jsonString, httpTY)
        if (httpTY == cut_page_info) {
            val mode = Gson().fromJson(jsonString, HomeCatInfo::class.java)
            binding.info = mode.data
            cutList.clear()
            mode.data.cut_order_list?.let {
                for (a in it) {
                    if (cutList.size != 2) {
                        cutList.add(a)
                    }
                }
                cutAdapter.notifyDataSetChanged()
            }
        }
    }


    override fun error(e: String?, jsonString: String?, httpTY: String?) {
        super.error(e, jsonString, httpTY)
        e?.show
    }

    override fun getLay(): Int {
        return R.layout.cut_home_fragment
    }
}